<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Table</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="css/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
    <link rel="stylesheet" href="css/table.css"/>
</head>

<body>
<div class="admin-main">

    <blockquote class="layui-elem-quote">
        <a href="javascript:;" class="layui-btn layui-btn-small" id="add">
            <i class="layui-icon">&#xe608;</i> 添加题库
        </a>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>数据列表</legend>
        <div class="layui-field-box">
            <table class="site-table table-hover">
                <thead>
                <tr>
                    <th style="width: 10px;"><input type="checkbox" id="selected-all"></th>
                    <th style="width: 100px;">类型</th>
                    <th>名称</th>
                    <th>说明</th>
                    <th style="width: 150px;">创建时间</th>
                    <th style="width: 150px;">操作</th>
                </tr>
                </thead>
                <tbody id="data_list">
                <tr>
                    <td colspan="6">Beginner</td>
                </tr>
                </tbody>
            </table>
        </div>
    </fieldset>
    <div class="admin-table-page">
        <div id="page" class="page">
        </div>
    </div>
</div>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script>
    function del_subjects(id, sid) {
        var $ = layui.jquery, layer = layui.layer;
        layer.confirm('确定要删除吗？', {
            icon: 3,
            title: "提示",
            btn: ['确定', '否'],
            cancel: function () {
                // 你点击右上角 X 取消后要做什么
                console.log("你点击右上角 X 取消后要做什么");
            },
            btn1: function (index) {
                $.get('question_subjects_del.do?id=' + id, null, function (form) {
                    get_subjects_data(sid);
                });
                layer.close(index);
            },
            btn2: function (index, layero) {
                console.log("否回调");
                layer.close(index);
            }
        });

    }

    function edit_subjects(id) {
        var $ = layui.jquery, layer = layui.layer;
        $.get('question_subjects_edit_view.do?id=' + id, null, function (form) {
            layer.open({
                type: 1,
                title: '新增题目',
                content: form,
                btn: ['保存', '取消'],
                area: ['620px', '100%'],
                maxmin: false,
                yes: function (index) {
                    var question_bank_id = $("#question_bank_id").val();
                    var s_question_type = $("#s_question_type").val();
                    var s_question_title = $("#s_question_title").val();
                    var s_question_choice = $("#s_question_choice").val();
                    var s_right_key = $("#s_right_key").val();
                    var s_fraction = $("#s_fraction").val();
                    $.post("question_subjects_edit.do", {
                        id: id,
                        type: s_question_type,
                        title: s_question_title,
                        choice: s_question_choice,
                        right_key: s_right_key,
                        fraction: s_fraction
                    }, function (result) {
                        parent.layer.alert(result);
                        get_subjects_data(question_bank_id);
                    });
                    layer.close(index);
                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
        });
    }

    function add_subjects(id) {
        var $ = layui.jquery, layer = layui.layer;
        $.get('question_subjects_add_view.do', null, function (form) {
            layer.open({
                type: 1,
                title: '新增题目',
                content: form,
                btn: ['保存', '取消'],
                area: ['620px', '100%'],
                maxmin: false,
                yes: function (index) {
                    var s_question_type = $("#s_question_type").val();
                    var s_question_title = $("#s_question_title").val();
                    var s_question_choice = $("#s_question_choice").val();
                    var s_right_key = $("#s_right_key").val();
                    var s_fraction = $("#s_fraction").val();
                    $.post("question_subjects_add.do", {
                        id: id,
                        type: s_question_type,
                        title: s_question_title,
                        choice: s_question_choice,
                        right_key: s_right_key,
                        fraction: s_fraction
                    }, function (result) {
                        parent.layer.alert(result);
                        get_subjects_data(id);
                    });
                    layer.close(index);
                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
        });
    }

    function question_subjects(id) {
        var $ = layui.jquery, layer = layui.layer;
        $.get('question_subjects.do?id=' + id, null, function (form) {
            layer.open({
                type: 1,
                title: '题目管理',
                content: form,
                btn: ['保存', '取消'],
                area: ['100%', '100%'],
                maxmin: false,
                yes: function (index) {

                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
        });
    }

    function edit(id) {
        var $ = layui.jquery, layer = parent.layer === undefined ? layui.layer : parent.layer;
        $.get('question_bank_edit_view.do?id=' + id, null, function (form) {
            layer.open({
                type: 1,
                title: '修改',
                content: form,
                btn: ['保存', '取消'],
                area: ['620px', '460px'],
                maxmin: false,
                yes: function (index) {
                    var question_type = $("#question_type", parent.document).val();
                    var question_name = $("#question_name", parent.document).val();
                    var question_explain = $("#question_explain", parent.document).val();
                    $.post("question_bank_edit.do", {
                        id: id,
                        question_type: question_type,
                        question_name: question_name,
                        question_explain: question_explain
                    }, function (result) {
                        parent.layer.alert(result);
                        get_data(1);
                    });
                    layer.close(index);
                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
        });
    }

    function del(id) {
        var $ = layui.jquery, layer = parent.layer === undefined ? layui.layer : parent.layer;
        layer.confirm('确定要删除吗？', {
            icon: 3,
            title: "提示",
            btn: ['确定', '否'],
            cancel: function () {
                // 你点击右上角 X 取消后要做什么
                console.log("你点击右上角 X 取消后要做什么");
            },
            btn1: function (index) {
                $.get('question_bank_del.do?id=' + id, null, function (form) {
                    get_data(1);
                });
                layer.close(index);
            },
            btn2: function (index, layero) {
                console.log("否回调");
                layer.close(index);
            }
        });
    }

    function get_data(page) {
        var $ = layui.jquery, layer = parent.layer === undefined ? layui.layer : parent.layer;
        $.post("question_bank_list_table.do", {page: page}, function (result) {
            $("#data_list").html(result);
        });
    }

    function get_subjects_data(id) {
        var $ = layui.jquery, layer = layui.layer;
        $.get('question_subjects_table.do?id=' + id, null, function (result) {
            $("#subjects_data_list").html(result);
        });
    }

    layui.config({
        base: 'plugins/layui/modules/'
    });
    layui.use(['icheck', 'laypage', 'layer'], function () {
        var $ = layui.jquery,
            laypage = layui.laypage,
            layer = parent.layer === undefined ? layui.layer : parent.layer;
        $('input').iCheck({
            checkboxClass: 'icheckbox_flat-green'
        });

        //page
        laypage({
            cont: 'page',
            pages: [(${page_count})],
            groups: 5 //连续显示分页数
            ,
            jump: function (obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                get_data(curr);
            }
        });

        $('#add').on('click', function () {
            $.get('question_bank_add_view.do', null, function (form) {
                layer.open({
                    type: 1,
                    title: '添加题库',
                    content: form,
                    btn: ['保存', '取消'],
                    area: ['620px', '460px'],
                    maxmin: false,
                    yes: function (index) {
                        var question_type = $("#question_type", parent.document).val();
                        var question_name = $("#question_name", parent.document).val();
                        var question_explain = $("#question_explain", parent.document).val();
                        $.post("question_bank_add.do", {
                            question_type: question_type,
                            question_name: question_name,
                            question_explain: question_explain
                        }, function (result) {
                            parent.layer.alert(result);
                            get_data(1);
                        });
                    },
                    full: function (elem) {
                        var win = window.top === window.self ? window : parent.window;
                        $(win).on('resize', function () {
                            var $this = $(this);
                            elem.width($this.width()).height($this.height()).css({
                                top: 0,
                                left: 0
                            });
                            elem.children('div.layui-layer-content').height($this.height() - 95);
                        });
                    }
                });
            });
        });

        $('.site-table tbody tr').on('click', function (event) {
            var $this = $(this);
            var $input = $this.children('td').eq(0).find('input');
            $input.on('ifChecked', function (e) {
                $this.css('background-color', '#EEEEEE');
            });
            $input.on('ifUnchecked', function (e) {
                $this.removeAttr('style');
            });
            $input.iCheck('toggle');
        }).find('input').each(function () {
            var $this = $(this);
            $this.on('ifChecked', function (e) {
                $this.parents('tr').css('background-color', '#EEEEEE');
            });
            $this.on('ifUnchecked', function (e) {
                $this.parents('tr').removeAttr('style');
            });
        });
        $('#selected-all').on('ifChanged', function (event) {
            var $input = $('.site-table tbody tr td').find('input');
            $input.iCheck(event.currentTarget.checked ? 'check' : 'uncheck');
        });
    });
</script>
</body>

</html>